package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@Api(tags = "套餐接口")
@RequestMapping("/admin/setmeal")
@RestController
@SuppressWarnings({"all"})
public class SetmealController {
    @Autowired
    SetmealService setmealService;


    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> SetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐的分页查询");
        PageResult pageResult = setmealService.Page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("套餐id查询接口")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Integer id) {
        log.info("根据id查询套餐{}", id);
        SetmealVO setmealVO = setmealService.selectById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐接口")
    @PutMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐");
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("新增套餐接口")
    @PostMapping
    @CacheEvict(cacheNames = "SetmealCache" ,key = "#setmealDTO.categoryId")
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐{}", setmealDTO);
        setmealService.insert(setmealDTO);
        return Result.success();
    }

    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    @ApiOperation("批量删除接口")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        int num = setmealService.delete(ids);
        if (num == StatusConstant.ON_SALE) {
            return Result.error("正在售卖的套餐不能删除");
        }
        return Result.success();

    }
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    @ApiOperation("是否售卖接口")
    @PostMapping("/status/{status}")
    public Result isSale(@PathVariable Integer status, Long id) {
        setmealService.isSale(status, id);
        return Result.success();
    }


}
